define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/teacher/index' + location.search,
                    add_url: 'choi/teacher/add',
                    edit_url: 'choi/teacher/edit',
                    del_url: 'choi/teacher/del',
                    multi_url: 'choi/teacher/multi',
                    table: 'teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'teacher_id',
                sortName: 'teacher_id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'teacher_id', title: __('Teacher_id'), operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'headeravatar', title: __('Headeravatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'phone', title: __('Phone')},
                        {field: 'teaching_year', title: __('Teaching_year'), operate: false},
                        // {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'longitude', title: __('Longitude')},
                        // {field: 'latitude', title: __('Latitude')},
                        // {field: 'sex', title: __('Sex'), searchList: {"0":__('Sex 0'),"1":__('Sex 1'),"2":__('Sex 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'profession', title: __('Profession'), searchList: {"0":__('Profession 0'),"1":__('Profession 1')}, formatter: Table.api.formatter.normal},

                        {field: 'money', title: __('Money'), operate:'BETWEEN', table: table, events: Table.api.events.operate, formatter: function (e, data) {
                            return `<a href="javascript:;" class="btn btn-xs btn-primary btn-money" data-id="`+data.teacher_id+`" title="删除">`+data.money+`</a>`;
                        }},
                        {field: 'images', title: __('详情图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'front_identity', title: __('Front_identity'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'back_identity', title: __('Back_identity'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'hand_identity', title: __('Hand_identity'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'certificate_code', title: __('Certificate_code')},
                        {field: 'certificate_image', title: __('Certificate_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'verify', title: __('Verify'), searchList: {"0":__('Verify 0'),"1":__('Verify 1'),"2":__('Verify 2')}, formatter: Table.api.formatter.status},
                        // {field: 'wxapp_openid', title: __('Wxapp_openid')},
                        // {field: 'token', title: __('Token')},
                        {field: 'grade', title: __('Grade'), searchList: {"0":__('Grade 0'),"1":__('Grade 1'),"2":__('Grade 2'),"3":__('Grade 3')}, formatter: Table.api.formatter.normal},
                        {field: 'address', title: __('Address'), operate: false},
                        // {field: 'online', title: __('Online')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                            var str = '';
                                str += ` <a href="javascript:;" class="btn btn-xs btn-warning btn-verify" data-id="`+data.teacher_id+`">身份审核</a> `
                                str += ` <a href="javascript:;" class="btn btn-xs btn-success btn-grade" data-id="`+data.teacher_id+`">等级</a>`;
                                str += ` <a href="javascript:;" class="btn btn-xs btn-success btn-experience" data-id="`+data.teacher_id+`">教学经历</a>`;
                                str +=  ` <a href="javascript:;" class="btn btn-xs btn-danger btn-delone" title="删除"><i class="fa fa-trash"></i></a> `;
                            return str;
                        }}
                    ]
                ]
            });

            //身份验证
            table.on('click', '.btn-verify', function () {
                var id = $(this).data('id');
                Fast.api.open('/keyu.php/choi/teacher/verify?id='+id, '审核身份证', {area: [50, 10]});
            })
            //等级
            table.on('click', '.btn-grade', function () {
                var id = $(this).data('id');
                Fast.api.open('/keyu.php/choi/teacher/grade?id='+id, '变更教师等级', {area: [50, 10]});
            })
            //账号明细
            table.on('click', '.btn-money', function () {
                var id = $(this).data('id');
                Fast.api.open('/keyu.php/choi/teacher_money_log/log?id='+id, '账号明细');
            })
            //教学经历
            table.on('click', '.btn-experience', function () {
                var id = $(this).data('id');
                Fast.api.open('/keyu.php/choi/teaching_experience/teacher?id='+id, '账号明细');
            })



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'choi/teacher/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'choi/teacher/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'choi/teacher/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        verify: function () {
            Controller.api.bindevent();
        },
        grade: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});